{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "from datetime import date, datetime\n",
    "\n",
    "## 类中实现 __get__、__set__、__delete__ 三个方法的任意一个，都可以作为属性描述符\n",
    "import numbers\n",
    "\n",
    "## 数据描述符\n",
    "class IntField:\n",
    "    def __get__(self, instance, owner):\n",
    "        return self.value\n",
    "        \n",
    "    def __set__(self, instance, value):\n",
    "        if not isinstance(value, numbers.Integral):\n",
    "            raise ValueError('int value need')\n",
    "        if value < 0:\n",
    "            raise ValueError('positive value need')\n",
    "        self.value = value\n",
    "        \n",
    "    def __delete__(self, instance):\n",
    "        pass\n",
    "\n",
    "\n",
    "## 非数据属性描述符\n",
    "class NonDataIntField:\n",
    "    def __get__(self, instance, owner):\n",
    "        return self.value\n",
    "\n",
    "\n",
    "class User:\n",
    "    age = IntField()\n",
    "\n",
    "\n",
    "# class User:\n",
    "#     def __init__(self, name, email, birthday):\n",
    "#         self.name = name\n",
    "#         self.email = email\n",
    "#         self.birthday = birthday\n",
    "#         self._age = 0\n",
    "        \n",
    "#     @property\n",
    "#     def age(self):\n",
    "#         return datetime.now().year - self.birthday.year\n",
    "    \n",
    "#     @age.setter\n",
    "#     def age(self, value):\n",
    "#         self._age = value\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "#     user = User('bobby', date(year=1987, month=1, day=1))\n",
    "    \n",
    "    user = User()\n",
    "    user.age = 30\n",
    "    print(user.age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 属性查找过程？？？"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
